load("//bazel:mongo_src_rules.bzl", "idl_generator", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
        "*.cpp",
    ]),
)

# Contains only the core ConnectionString functionality, *not* the ability to
# call connect() and return a DBClientBase* back. For that you need to link
# against the 'clientdriver_network' library.
mongo_cc_library(
    name = "connection_string",
    srcs = [
        "connection_string.cpp",
        "mongo_uri.cpp",
        "//src/mongo/db/auth:sasl_command_constants.h",
        "//src/mongo/util:dns_name.h",
    ],
    hdrs = [
        "connection_string.h",
        "mongo_uri.h",
    ],
    deps = [
        "//src/mongo/util:dns_query",
        "//src/mongo/util/net:network",
        "//src/mongo/util/options_parser",  # TODO(SERVER-93876): Remove.
    ],
)

mongo_cc_library(
    name = "internal_auth",
    srcs = [
        "internal_auth.cpp",
    ],
    hdrs = [
        "internal_auth.h",
    ],
    deps = [
        ":connection_string",
        "//src/mongo/bson/util:bson_extract",
        "//src/mongo/db/auth",
        "//src/mongo/util:md5",
    ],
)

idl_generator(
    name = "sasl_aws_protocol_common_gen",
    src = "sasl_aws_protocol_common.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "sasl_aws_client_protocol_gen",
    src = "sasl_aws_client_protocol.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "sasl_oidc_client_types_gen",
    src = "sasl_oidc_client_types.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "sasl_aws_client_options_gen",
    src = "sasl_aws_client_options.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

# Client-side SASL source necessary for native client SASL session.
# Includes base types and OIDC, PLAIN, SCRAM, and AWS (only if TLS configured)
# client mechs.
mongo_cc_library(
    name = "native_sasl_client",
    srcs = [
        "native_sasl_client_session.cpp",
        "sasl_client_authenticate.cpp",
        "sasl_client_authenticate_impl.cpp",
        "sasl_client_conversation.cpp",
        "sasl_client_session.cpp",
        "sasl_oidc_client_conversation.cpp",
        "sasl_oidc_client_types_gen",
        "sasl_plain_client_conversation.cpp",
        "sasl_scram_client_conversation.cpp",
    ] + select({
        "//bazel/config:ssl_enabled": [
            "sasl_aws_client_conversation.cpp",
            "sasl_aws_client_options_gen",
            "sasl_x509_client_conversation.cpp",
        ],
        "//conditions:default": [],
    }),
    hdrs = [
        "native_sasl_client_session.h",
        "sasl_aws_client_options.h",
        "sasl_client_authenticate.h",
        "sasl_client_conversation.h",
        "sasl_client_session.h",
        "sasl_oidc_client_conversation.h",
        "sasl_oidc_client_params.h",
        "sasl_plain_client_conversation.h",
        "sasl_scram_client_conversation.h",
        "sasl_x509_client_conversation.h",
        "scram_client_cache.h",
        "//src/mongo/crypto:mechanism_scram.h",
    ] + select({
        "//bazel/config:ssl_enabled": [
            "sasl_aws_client_conversation.h",
        ],
        "//conditions:default": [],
    }),
    deps = [
        ":connection_string",
        "//src/mongo/base:secure_allocator",  # TODO(SERVER-93876): Remove.
        "//src/mongo/bson/util:bson_extract",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:server_base",
        "//src/mongo/db/auth:sasl_mechanism_protocol",
        "//src/mongo/db/commands:server_status_core",
        "//src/mongo/executor:remote_command",  # TODO(SERVER-93876): Remove.
        "//src/mongo/rpc:command_status",
        "//src/mongo/rpc:metadata",  # TODO(SERVER-93876): Remove.
        "//src/mongo/shell:program_runner",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util:icu",
        "//src/mongo/util:md5",
        "//src/mongo/util/net:http_client",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/net:network",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/options_parser",
    ] + select({
        "//bazel/config:ssl_enabled": [
            ":sasl_aws_client",
        ],
        "//conditions:default": [],
    }),
)

mongo_cc_library(
    name = "authentication",
    srcs = [
        "authenticate.cpp",
    ],
    hdrs = [
        "authenticate.h",
    ],
    deps = [
        ":connection_string",  # TODO(SERVER-93876): Remove.
        ":internal_auth",  # TODO(SERVER-93876): Remove.
        ":native_sasl_client",
        "//src/mongo/bson/util:bson_extract",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:server_base",
        "//src/mongo/db/auth",  # TODO(SERVER-93876): Remove.
        "//src/mongo/executor:remote_command",  # TODO(SERVER-93876): Remove.
    ],
)

mongo_cc_library(
    name = "async_client",
    srcs = [
        "async_client.cpp",
    ],
    hdrs = [
        "async_client.h",
    ],
    deps = [
        ":authentication",
        "//src/mongo/db:connection_health_metrics_parameter",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:server_feature_flags",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:wire_version",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/commands:test_commands_enabled",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/stats:counters",  # TODO(SERVER-93876): Remove.
        "//src/mongo/executor:egress_connection_closer_manager",
        "//src/mongo/rpc",
        "//src/mongo/rpc:command_status",  # TODO(SERVER-93876): Remove.
        "//src/mongo/transport:message_compressor",  # TODO(SERVER-93876): Remove.
        "//src/mongo/transport:transport_layer_common",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util:fail_point",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/net:ssl_manager",  # TODO(SERVER-93876): Remove.
    ],
)

idl_generator(
    name = "sasl_sspi_options_gen",
    src = "sasl_sspi_options.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "client_api_version_parameters_gen",
    src = "client_api_version_parameters.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "client_transient_tls_parameters_gen",
    src = "client_transient_tls_parameters.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "mongo_shell_options_gen",
    src = "mongo_shell_options.idl",
    deps = [
        ":client_api_version_parameters_gen",
        ":client_transient_tls_parameters_gen",
        "//src/mongo/db:basic_types_gen",
    ],
)

mongo_cc_library(
    name = "clientdriver_minimal",
    srcs = [
        "client_api_version_parameters_gen",
        "client_transient_tls_parameters_gen",
        "dbclient_base.cpp",
        "dbclient_cursor.cpp",
        "index_spec.cpp",
        "mongo_shell_options_gen",
    ],
    hdrs = [
        "dbclient_base.h",
        "dbclient_cursor.h",
        "index_spec.h",
    ],
    deps = [
        ":authentication",
        ":connection_string",  # TODO(SERVER-93876): Remove.
        ":read_preference",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:dbmessage",
        "//src/mongo/db:server_base",
        "//src/mongo/db:wire_version",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/pipeline:aggregation_request_helper",
        "//src/mongo/db/query:command_request_response",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/query:query_request",  # TODO(SERVER-93876): Remove.
        "//src/mongo/rpc",  # TODO(SERVER-93876): Remove.
        "//src/mongo/rpc:command_status",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/net:ssl_manager",
    ],
)

idl_generator(
    name = "global_conn_pool_gen",
    src = "global_conn_pool.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

mongo_cc_library(
    name = "clientdriver_network",
    srcs = [
        "connection_string_connect.cpp",
        "connpool.cpp",
        "dbclient_connection.cpp",
        "dbclient_rs.cpp",
        "dbclient_session.cpp",
        "global_conn_pool.cpp",
        "global_conn_pool_gen",
        "mongo_uri_connect.cpp",
        "replica_set_change_notifier.cpp",
        "replica_set_monitor.cpp",
        "replica_set_monitor_manager.cpp",
        "replica_set_monitor_stats.cpp",
        "server_discovery_monitor.cpp",
        "server_ping_monitor.cpp",
        "streamable_replica_set_monitor.cpp",
        "streamable_replica_set_monitor_discovery_time_processor.cpp",
        "streamable_replica_set_monitor_error_handler.cpp",
        "streamable_replica_set_monitor_query_processor.cpp",
    ] + select({
        "//bazel/config:build_grpc_enabled": [
            "dbclient_grpc_stream.cpp",
        ],
        "//conditions:default": [],
    }),
    hdrs = [
        "connpool.h",
        "dbclient_connection.h",
        "dbclient_rs.h",
        "dbclient_session.h",
        "global_conn_pool.h",
        "replica_set_change_notifier.h",
        "replica_set_monitor.h",
        "replica_set_monitor_interface.h",
        "replica_set_monitor_manager.h",
        "replica_set_monitor_stats.h",
        "server_discovery_monitor.h",
        "server_ping_monitor.h",
        "streamable_replica_set_monitor.h",
        "streamable_replica_set_monitor_discovery_time_processor.h",
        "streamable_replica_set_monitor_error_handler.h",
        "streamable_replica_set_monitor_query_processor.h",
    ] + select({
        "//bazel/config:build_grpc_enabled": [
            "dbclient_grpc_stream.h",
        ],
        "//conditions:default": [],
    }),
    deps = [
        ":clientdriver_minimal",
        ":read_preference",  # TODO(SERVER-93876): Remove.
        ":replica_set_monitor_server_parameters",
        "//src/mongo/client/sdam",
        "//src/mongo/db:server_base",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db:server_feature_flags",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/auth",  # TODO(SERVER-93876): Remove.
        "//src/mongo/db/commands:test_commands_enabled",  # TODO(SERVER-93876): Remove.
        "//src/mongo/executor:connection_pool_stats",
        "//src/mongo/executor:network_interface",
        "//src/mongo/executor:network_interface_factory",
        "//src/mongo/executor:network_interface_thread_pool",
        "//src/mongo/executor:thread_pool_task_executor",
        "//src/mongo/transport:message_compressor",  # TODO(SERVER-93876): Remove.
        "//src/mongo/transport:transport_layer_manager",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util:background_job",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util:md5",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/net:network",  # TODO(SERVER-93876): Remove.
        "//src/mongo/util/net:ssl_manager",
    ] + select({
        "//bazel/config:build_grpc_enabled": [
            "//src/mongo/transport/grpc:grpc_transport_layer",
        ],
        "//conditions:default": [],
    }),
)

idl_generator(
    name = "replica_set_monitor_server_parameters_gen",
    src = "replica_set_monitor_server_parameters.idl",
)

idl_generator(
    name = "read_preference_setting_gen",
    src = "read_preference_setting.idl",
    deps = [
        ":read_preference_gen",
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "read_preference_gen",
    src = "read_preference.idl",
    deps = [
        ":hedging_mode_gen",
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "hedging_mode_gen",
    src = "hedging_mode.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

mongo_cc_library(
    name = "sasl_aws_common",
    srcs = [
        ":sasl_aws_protocol_common_gen",
    ],
    hdrs = [
        "//src/mongo/db:basic_types_gen",
        "//src/mongo/db/query:explain_verbosity_gen",
    ],
    deps = [
        "//src/mongo/db:server_base",
    ],
)

mongo_cc_library(
    name = "replica_set_monitor_server_parameters",
    srcs = [
        "replica_set_monitor_server_parameters.cpp",
        ":replica_set_monitor_server_parameters_gen",
    ],
    hdrs = [
        "replica_set_monitor_server_parameters.h",
    ],
    deps = [
        "//src/mongo/db:server_base",
    ],
)

mongo_cc_library(
    name = "sasl_aws_client",
    srcs = [
        "sasl_aws_client_protocol.cpp",
        "sasl_aws_client_protocol_gen",
    ],
    hdrs = [
        "sasl_aws_client_protocol.h",
        "sasl_aws_protocol_common.h",
        "sasl_aws_protocol_common_gen",
        "//src/mongo/bson:bson_validate_gen",
        "//src/mongo/util:kms_message_support.h",
    ],
    header_deps = [
        "//src/mongo/db/repl:oplog_buffer_batched_queue",
    ],
    target_compatible_with = select({
        "//bazel/config:ssl_enabled": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        "sasl_aws_common",
        "//src/mongo/bson:bson_validate",
        "//src/mongo/db:server_base",
        "//src/third_party/libmongocrypt:mongocrypt",
    ],
)

mongo_cc_library(
    name = "read_preference",
    srcs = [
        "read_preference_decoration.cpp",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/db:service_context",
    ],
)

mongo_cc_library(
    name = "remote_command_retry_scheduler",
    srcs = [
        "remote_command_retry_scheduler.cpp",
    ],
    hdrs = [
        "remote_command_retry_scheduler.h",
    ],
    deps = [
        "//src/mongo:base",  # TODO(SERVER-93876): Remove.
        "//src/mongo/executor:task_executor_interface",
    ],
)

mongo_cc_library(
    name = "fetcher",
    srcs = [
        "fetcher.cpp",
    ],
    hdrs = [
        "fetcher.h",
    ],
    deps = [
        ":remote_command_retry_scheduler",
        "//src/mongo/db:server_base",
        "//src/mongo/db/query/client_cursor",
        "//src/mongo/executor:task_executor_interface",  # TODO(SERVER-93876): Remove.
        "//src/mongo/rpc:command_status",
    ],
)

mongo_cc_library(
    name = "remote_command_targeter",
    srcs = [
        "remote_command_targeter_factory_impl.cpp",
        "remote_command_targeter_rs.cpp",
        "remote_command_targeter_standalone.cpp",
    ],
    hdrs = [
        "remote_command_targeter_factory_impl.h",
        "remote_command_targeter_rs.h",
        "remote_command_targeter_standalone.h",
    ],
    deps = [
        ":clientdriver_network",
        "//src/mongo/db:service_context",
    ],
)

# Client-side SASL source necessary for mongo shell.
# Includes Cyrus SASL, OIDC, PLAIN, and AWS (if TLS support is on)
# support. Depends on sasl_client_native.
mongo_cc_library(
    name = "cyrus_sasl_client",
    srcs = [
        "cyrus_sasl_client_session.cpp",
        "sasl_sspi.cpp",
        "sasl_sspi_options.cpp",
        "sasl_sspi_options_gen",
    ],
    hdrs = [
        "cyrus_sasl_client_session.h",
        "sasl_sspi_options.h",
    ],
    linkopts = [
        "-lsasl2",
    ] + select({
        "@platforms//os:windows": [
            "secur32.lib",
            "sasl2.lib",
        ],
        "//conditions:default": [],
    }),
    target_compatible_with = select({
        "//bazel/config:use_sasl_client_enabled": [],
        "//bazel/config:enterprise_feature_sasl_enabled": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        "native_sasl_client",
    ],
)

mongo_cc_library(
    name = "remote_command_targeter_mock",
    srcs = [
        "remote_command_targeter_factory_mock.cpp",
        "remote_command_targeter_mock.cpp",
    ],
    hdrs = [
        "remote_command_targeter.h",
        "remote_command_targeter_factory.h",
        "remote_command_targeter_factory_mock.h",
        "remote_command_targeter_mock.h",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "replica_set_monitor_test_helper",
    srcs = [
        "streamable_replica_set_monitor_for_testing.cpp",
    ],
    hdrs = [
        "streamable_replica_set_monitor_for_testing.h",
    ],
    deps = [
        ":replica_set_monitor_protocol_test_util",
        "//src/mongo/client:clientdriver_network",
        "//src/mongo/client/sdam:sdam_test_util",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "replica_set_monitor_protocol_test_util",
    srcs = [
        "replica_set_monitor_protocol_test_util.cpp",
    ],
    hdrs = [
        "replica_set_monitor_protocol_test_util.h",
    ],
    deps = [
        "clientdriver_network",
    ],
)

mongo_cc_library(
    name = "dbclient_mockcursor",
    srcs = ["dbclient_mockcursor.cpp"],
    hdrs = ["dbclient_mockcursor.h"],
    deps = ["clientdriver_minimal"],
)

mongo_cc_unit_test(
    name = "client_test",
    srcs = [
        "async_remote_command_targeter_adapter_test.cpp",
        "authenticate_test.cpp",
        "connection_string_test.cpp",
        "dbclient_cursor_test.cpp",
        "fetcher_test.cpp",
        "index_spec_test.cpp",
        "mongo_uri_test.cpp",
        "read_preference_test.cpp",
        "remote_command_retry_scheduler_test.cpp",
        "replica_set_monitor_server_parameters_test.cpp",
        "server_discovery_monitor_expedited_test.cpp",
        "server_discovery_monitor_test.cpp",
        "server_ping_monitor_test.cpp",
        "streamable_replica_set_monitor_discovery_time_processor_test.cpp",
        "streamable_replica_set_monitor_error_handler_test.cpp",
        "//src/mongo/util:executor_test_util.h",
        "//src/mongo/util:future_test_utils.h",
    ],
    tags = ["mongo_unittest_second_group"],
    target_compatible_with = select({
        "//bazel/config:use_wiredtiger_enabled": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":authentication",
        ":clientdriver_minimal",
        ":clientdriver_network",
        ":connection_string",
        ":fetcher",
        ":read_preference",
        ":remote_command_retry_scheduler",
        ":remote_command_targeter_mock",
        ":replica_set_monitor_protocol_test_util",
        "//src/mongo/client/sdam",
        "//src/mongo/client/sdam:sdam_test_util",
        "//src/mongo/db:server_base",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/db/auth:authmocks",
        "//src/mongo/dbtests:mocklib",
        "//src/mongo/executor:network_interface_mock",
        "//src/mongo/executor:task_executor_test_fixture",
        "//src/mongo/executor:thread_pool_task_executor_test_fixture",
        "//src/mongo/idl:server_parameter_test_util",
        "//src/mongo/rpc:command_status",
        "//src/mongo/transport:transport_layer_common",
        "//src/mongo/transport:transport_layer_egress_init",
        "//src/mongo/unittest:task_executor_proxy",
        "//src/mongo/util:md5",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_unit_test(
    name = "client_rs_test",
    srcs = [
        "dbclient_rs_test.cpp",
    ],
    tags = ["mongo_unittest_third_group"],
    deps = [
        ":clientdriver_network",
        ":replica_set_monitor_test_helper",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/dbtests:mocklib",
        "//src/mongo/idl:server_parameter_test_util",
        "//src/mongo/util:clock_source_mock",
    ],
)

mongo_cc_unit_test(
    name = "dbclient_grpc_stream_test",
    srcs = [
        "dbclient_grpc_stream_test.cpp",
    ],
    tags = ["mongo_unittest_second_group"],
    target_compatible_with = select({
        "//bazel/config:build_grpc_enabled": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":clientdriver_network",
        "//src/mongo/db:service_context_non_d",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/transport/grpc:grpc_transport_mock",
        "//src/mongo/util:version_impl",
    ],
)

mongo_cc_unit_test(
    name = "scoped_db_connection_pool_test",
    srcs = ["scoped_db_connection_test.cpp"],
    tags = ["mongo_unittest_fourth_group"],
    deps = [
        ":clientdriver_network",
        "//src/mongo/dbtests:mocklib",
    ],
)

mongo_cc_unit_test(
    name = "dbclient_connection_test",
    srcs = [
        "dbclient_connection_test.cpp",
    ],
    tags = ["mongo_unittest_second_group"],
    deps = [
        ":clientdriver_network",
        "//src/mongo/util:version_impl",
    ],
)

# TODO(SERVER-96852): Remove cycle created by moving //src/mongo/client:connection_string.h to //src/mongo/client:connection_string
filegroup(
    name = "connection_string_hdrs",
    srcs = [":connection_string.h"],
)
